home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Hacking & Misc / bundle of exploits.sit / bundle of exploits / ident-scan.c < prev    next >
C/C++ Source or Header  |  1998-07-17  |  4KB  |  137 lines

  1. /*
  2.  *   ident-scan [v0.15]
  3.  *   This TCP scanner has the additional functionality of retrieving
  4.  *   the username that owns the daemon running on the specified port.
  5.  *   It does this by by attempting to connect to a TCP port, and if it
  6.  *   succeeds, it will send out an ident request to identd on the
  7.  *   remote host.  I believe this to be a flaw in the design of the
  8.  *   protocol, and if it is the developers intent to allow 'reverse'
  9.  *   idents, then it should have been stated clearer in the
  10.  *   rfc(rfc1413).
  11.  *
  12.  *   USES:
  13.  *   It can be useful to determine who is running daemons on high ports
  14.  *   that can be security risks.  It can also be used to search for
  15.  *   misconfigurations such as httpd running as root, other daemons
  16.  *   running under the wrong uids.
  17.  *
  18.  *   COMPILES:  Compiles fine under Linux, BSDI and SunOS 4.1.x.
  19.  *
  20.  *   Dave Goldsmith
  21.  *   <daveg@escape.com>
  22.  *   02/11/1996
  23.  */
  24.  
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <sys/types.h>
  28. #include <sys/socket.h>
  29. #include <errno.h>
  30. #include <fcntl.h>
  31. #include <netinet/in.h>
  32. #include <netdb.h>
  33. #include <unistd.h>
  34.  
  35. enum errlist
  36. {
  37.   BAD_ARGS,BAD_HOST,NO_IDENT,SOCK_ERR
  38. };
  39.  
  40. void
  41. usage(error)
  42. enum errlist error;
  43. {
  44.   fprintf(stderr,"ident-scan: ");
  45.   switch(error)
  46.   {
  47.     case BAD_ARGS: fprintf(stderr,"usage: ident-scan hostname [low port] [hi port]\n");
  48.                    break;
  49.     case BAD_HOST: fprintf(stderr,"error: cant resolve hostname\n");
  50.                    break;
  51.     case NO_IDENT: fprintf(stderr,"error: ident isnt running on host\n");
  52.                    break;
  53.     case SOCK_ERR: fprintf(stderr,"error: socket() failed\n");
  54.                    break;
  55.   }
  56.   exit(-1);
  57. }
  58.  
  59. struct hostent *
  60. fill_host(machine,host)
  61. char *machine;
  62. struct hostent *host;
  63. {
  64.  
  65.   if ((host=gethostbyname(machine))==NULL)
  66.   {
  67.      if ((host=gethostbyaddr(machine,4,AF_INET))==NULL)
  68.         return(host);
  69.   }
  70.   return(host);
  71. }
  72.  
  73. int
  74. main(argc,argv)
  75. int argc;
  76. char **argv;
  77. {
  78.   struct sockaddr_in forconnect,forport,forident;
  79.   int i,sockfd,identfd,len=sizeof(forport),hiport=9999,loport=1,curport;
  80.   struct servent *service;
  81.   struct hostent *host;
  82.   char identbuf[15], recieved[85], *uid;
  83.  
  84.   if ((argc<2) || (argc>4))
  85.     usage(BAD_ARGS);
  86.   if (argc>2)
  87.      loport=atoi(argv[2]);
  88.   if (argc>3)
  89.      hiport=atoi(argv[3]);
  90.   if ((host=fill_host(argv[1],host))==NULL)
  91.     usage(BAD_HOST);
  92.   forconnect.sin_family=host->h_addrtype;
  93.   forconnect.sin_addr.s_addr=*((long *)host->h_addr);
  94.   forident.sin_family=host->h_addrtype;
  95.   forident.sin_addr.s_addr=*((long *)host->h_addr);
  96.   forident.sin_port=htons(113);
  97.  
  98.   if ((identfd=socket(AF_INET,SOCK_STREAM,0))== -1)
  99.      usage(SOCK_ERR);
  100.   if ((connect(identfd,(struct sockaddr *)&forident,sizeof(forident)))!=0)
  101.      usage(NO_IDENT);
  102.   close(identfd);
  103.  
  104.   for(curport=loport;curport<=hiport;curport++)
  105.   {
  106.      for(i=0;i!=85;i++)
  107.         recieved[i]='\0';
  108.      forconnect.sin_port=htons(curport);
  109.      if ((sockfd=socket(AF_INET,SOCK_STREAM,0))== -1)
  110.         usage(SOCK_ERR);
  111.  
  112.      if (connect(sockfd,(struct sockaddr *)&forconnect,sizeof(forconnect))==0)
  113.      {
  114.        if (getsockname(sockfd,(struct sockaddr *)&forport,&len)==0)
  115.        {
  116.           if ((identfd=socket(AF_INET,SOCK_STREAM,0))== -1)
  117.              usage(SOCK_ERR);
  118.           if (connect(identfd,(struct sockaddr *)&forident,sizeof(forident))==0)
  119.           {
  120.              sprintf(identbuf,"%u,%u",htons(forconnect.sin_port),
  121.                 htons(forport.sin_port));
  122.  
  123.              write(identfd,identbuf,strlen(identbuf)+1);
  124.              read(identfd,recieved,80);
  125.              recieved[strlen(recieved)-1]='\0';
  126.              uid=strrchr(recieved,' ');
  127.              service=getservbyport(forconnect.sin_port,"tcp");
  128.              printf("Port: %3d\tService: %10s\tUserid: %s\n",curport,
  129.                 (service==NULL)?"(?)":service->s_name,uid);
  130.           }
  131.        }
  132.     }
  133.     close(sockfd);
  134.     close(identfd);
  135.   }
  136. }
  137.